<?php
  include_once($_SERVER['DOCUMENT_ROOT']."/swo.cfg.php");
  require_once("$root_path/lib/dbConnect.php");
  class DBUtil
  {
  	private $mysqli;
	function __construct() 
	{
	   $dbManager=new DBManager();
	   $this->mysqli=$dbManager->getDBConnection();
    }

    public function executeQuery($sql)
	 {
	    $result = $this->mysqli->query($sql);
		 while($row=$result->fetch_object())
		 {
		   $resultJSON[]= $row;
		 }
         //secho "json Data".$resultJSON;
		 return $resultJSON;
	 }

    //Return the Data As JSON Object
	public function getAllRecords($tableName)
	  {
	     $sql="select * from ".$tableName." where status=0";
		 $result = $this->mysqli->query($sql);
		 while($row=$result->fetch_object())
		 {
		   $resultJSON[]= $row;
		 }
         //secho "json Data".$resultJSON;
		 return $resultJSON;
	 }
   
     public function dynamicInsert($requestArray,$tableName)
	 {
	     $sql = "insert into $tableName set".$this->generateInsertData($requestArray);
		 //echo "Generated Sql $sql";
		 $result = $this->mysqli->query($sql);
		 session_start();
		 if ($result != "")
		 {
		    $_SESSION["statusMsg"]="Success";
		 }
		   else{
		    $_SESSION["statusMsg"]="failed";
		   }
	 } 
	 
	 public function dynamicDelete($key,$tableName)
	 {
	     $sql = "update $tableName set status=1 where id=$key";
		 $result = $this->mysqli->query($sql);
		 session_start();
		 if ($result != "")
		 {
		    $_SESSION["statusMsg"]="Success";
		 }
		   else{
		    $_SESSION["statusMsg"]="failed";
		   }
	 } 
	 
	 public function dynamicUpdate($requestArray,$tableName,$id,$excludeFields)
	 {
	     $sql = "update $tableName set ".$this->generateUpdateData($requestArray,$excludeFields)." where id=$id";
		 $result = $this->mysqli->query($sql);
		 //echo "update Sql $sql";
		 //echo "Update Result $result";
		 session_start();
		 if ($result != 0)
		 {
		    $_SESSION["statusMsg"]="Success";
		 }
		   else{
		    $_SESSION["statusMsg"]="failed";
		   }
	 } 
	 
	 private function generateInsertData($requestArray)
	 {
	    $sql=" ";
		foreach($requestArray as $key=>$value)
		{
	    	$sql.= "`$key`='$value',";
	    } 
		//substr to remove the submit button value data
	    return substr($sql,0,-1);
	 }
	 
	 private function generateUpdateData($requestArray,$excludeFields)
	 {
	    $sql=" ";
		foreach($requestArray as $key=>$value)
		{
		    if ($key == $excludeFields)
			  continue;
	    	$sql.= "`$key`='$value',";
	    } 
		//substr to remove the last comma
	    return substr($sql,0,-1);
	 }
	 
	 public function getTableCounter($tableName)
	 {
	     $sql="select count(*) as total from ".$tableName;
		 $result = $this->mysqli->query($sql);
		 $row=$result->fetch_object();
	     return $row->total;
	 }
	 
	 public function getRecordById($tableName,$id)
	 {
	     $sql="select * from ".$tableName." where id=$id";
		 $result = $this->mysqli->query($sql);
		 $row=$result->fetch_object();
	     return $row;
	 }
	 

	  
	 function __destruct() 
	{
	  $this->mysqli->close();
	  //echo "<br />Connection Closed at dbutil<br />";
	}
 }


?>